package com.atguigu.paymentdemo.service.impl;

import com.atguigu.paymentdemo.entity.OrderInfo;
import com.atguigu.paymentdemo.entity.PaymentInfo;
import com.atguigu.paymentdemo.enums.OrderStatus;
import com.atguigu.paymentdemo.mapper.OrderInfoMapper;
import com.atguigu.paymentdemo.service.OrderInfoService;
import com.atguigu.paymentdemo.service.PaymentInfoService;
import com.atguigu.paymentdemo.util.OrderNoUtils;
import com.atguigu.paymentdemo.vo.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.refund.model.Refund;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.*;

@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
    @Autowired
    private  WxNavicateService wxNavicateService;
    @Autowired
    private PaymentInfoService paymentInfoService;


    @Override
    @Transactional
    public R creatOrder(String id) {
        // 创建订单
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCreateTime(new Date());
        orderInfo.setUserId(1l);
        orderInfo.setTitle("测试");
        orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());
        orderInfo.setOrderNo(OrderNoUtils.getOrderNo());
        orderInfo.setProductId(Long.valueOf(id));
        orderInfo.setTotalFee(1);
        boolean save = save(orderInfo);
        if (!save) {
           return R.error();
        }
        // 调用微信支付  返回url
        String codeUrl = wxNavicateService.order(orderInfo);
        orderInfo.setCodeUrl(codeUrl);
        lambdaUpdate().eq(OrderInfo::getId,orderInfo.getId())
                .set(OrderInfo::getCodeUrl,codeUrl)
                .update();
        Map<String, Object> map = new HashMap<>();
        map.put("codeUrl",codeUrl);
        map.put("orderNo", orderInfo.getId());
        R r = R.ok();
        r.setData(map);
        return r;
    }

    @Override
    public R orderList() {
        List<OrderInfo> list = list();
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",list);
        R r = R.ok();
        r.setData(map);
        return r;
    }

    @Override
    public void updatePayStatusByOrderNo(String orderNo, String type) {
        lambdaUpdate().eq(OrderInfo::getOrderNo,orderNo)
                .set(OrderInfo::getOrderStatus,type)
                .update();
    }

    @Override
    public R cancleOrder(String outOrderId) {
        // 关闭订单
        wxNavicateService.closeOrder(outOrderId);
        // 修改订单状态
        updatePayStatusByOrderNo(outOrderId,OrderStatus.CANCEL.getType());
        return null;
    }

    @Override
    public R refundOrder(String outOrderId) {
        // 查询对应支付信息
        PaymentInfo paymentInfo = paymentInfoService.findByOutOrderId(outOrderId);
        // 调用接口进行退款
        Refund refund = wxNavicateService.refundOrder(paymentInfo);
        if (refund.getStatus().toString().equals(OrderStatus.SUCCESS.getType())) {
            // 修改订单状态
            updatePayStatusByOrderNo(outOrderId,OrderStatus.REFUND_SUCCESS.getType());
            return R.ok();
        }else {
            updatePayStatusByOrderNo(outOrderId,OrderStatus.REFUND_PROCESSING.getType());
            return R.ok();
        }
    }

    /**
     * 下载账单
     * @param date
     * @return
     */
    @Override
    public R downloadOrder(String date) {
        // 申请交易账单
        String url = wxNavicateService.applTradeText(date);
        // 下载交易账单
        String s;
        try {
             s = wxNavicateService.downloadTradeText(url);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return new R().ok().data("result",s);
    }
}
